package org.dromara.netbar.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.constant.NetbarConstants;
import org.dromara.common.core.utils.AssertUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.netbar.domain.vo.NetbarGiftPackVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.netbar.domain.vo.NetbarAutoGrantVo;
import org.dromara.netbar.domain.bo.NetbarAutoGrantBo;
import org.dromara.netbar.service.INetbarAutoGrantService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 自动发放策略
 *
 * @author Lion Li
 * @date 2025-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/netbar/autoGrant")
public class NetbarAutoGrantController extends BaseController {

    private final INetbarAutoGrantService netbarAutoGrantService;

    /**
     * 查询自动发放策略列表
     */
    @SaCheckPermission("netbar:autoGrant:list")
    @GetMapping("/list")
    public TableDataInfo<NetbarAutoGrantVo> list(NetbarAutoGrantBo bo, PageQuery pageQuery) {
        return netbarAutoGrantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出自动发放策略列表
     */
    @SaCheckPermission("netbar:autoGrant:export")
    @Log(title = "自动发放策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NetbarAutoGrantBo bo, HttpServletResponse response) {
        List<NetbarAutoGrantVo> list = netbarAutoGrantService.queryList(bo);
        ExcelUtil.exportExcel(list, "自动发放策略", NetbarAutoGrantVo.class, response);
    }

    /**
     * 获取自动发放策略详细信息
     *
     * @param autoGrantId 主键
     */
    @SaCheckPermission("netbar:autoGrant:query")
    @GetMapping("/{autoGrantId}")
    public R<NetbarAutoGrantVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long autoGrantId) {
        return R.ok(netbarAutoGrantService.queryById(autoGrantId));
    }

    /**
     * 新增自动发放策略
     */
    @SaCheckPermission("netbar:autoGrant:add")
    @Log(title = "自动发放策略", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NetbarAutoGrantBo bo) {
        return toAjax(netbarAutoGrantService.insertByBo(bo));
    }

    /**
     * 修改自动发放策略
     */
    @SaCheckPermission("netbar:autoGrant:edit")
    @Log(title = "自动发放策略", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NetbarAutoGrantBo bo) {
        return toAjax(netbarAutoGrantService.updateByBo(bo));
    }

    /**
     * 删除自动发放策略
     *
     * @param autoGrantIds 主键串
     */
    @SaCheckPermission("netbar:autoGrant:remove")
    @Log(title = "自动发放策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{autoGrantIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] autoGrantIds) {
        return toAjax(netbarAutoGrantService.deleteWithValidByIds(List.of(autoGrantIds), true));
    }

    /**
     * 根据余额和会员获取能获得的礼包
     */
    @PostMapping("/listGiftPackByBalanceAndMemberId")
    public R<List<NetbarAutoGrantVo>> listGiftPackByBalanceAndMemberId(@RequestBody NetbarAutoGrantBo bo){
        AssertUtils.notNull(bo.getMemberId(), String.format("会员信息有误：%s", NetbarConstants.MEMBER_ID_ERROR));
        AssertUtils.notNull(bo.getMinRechargeAmount(), String.format("充值金额有误：%s", NetbarConstants.AMOUNT_ERROR));
        AssertUtils.isTrue(bo.getMinRechargeAmount() > 0, String.format("充值金额有误：%s", NetbarConstants.AMOUNT_ERROR));
        return R.ok(netbarAutoGrantService.listGiftPackByBalanceAndMemberId(bo.getMinRechargeAmount(), LoginHelper.getTenantId(), bo.getMemberId()));
    }
}
